package com.finance.system.support.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class TimeFormatUtil {
	private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	private static String YYYY_MM_DD_HH_MM_SS_FFF = "yyyy-MM-dd HH:mm:ss.SSS";
	private static String YYYY_MM_DD = "yyyy-MM-dd";
	private static String YYYY_MM_DD2 ="yyyy/MM/dd";
	private static String YYYY_M_D ="yyyy年M月d日";
	public static final String YYYY_MM = "yyyy-MM";
	private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	private static String YYYYMMMDD_SIMPLE = "yyyyMMdd";

	public static String defaultFormat() {
		return formatYYYYMMDDHHMMSS();
	}

	private static DateTimeFormatter dateTimeFormatterToYYYYMMDDHHMMSS =
			DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
	/**
	 * @return 2017-02-21 09:32:07
	 */
	public static String formatYYYYMMDDHHMMSSFFF() {
		return format(YYYY_MM_DD_HH_MM_SS_FFF, new Date());
	}

	public static String formatYYYYMMDDHHMMSS() {
		return format(YYYY_MM_DD_HH_MM_SS, new Date());
	}


	private static String format(String formatStr, Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
		return formatter.format(date);
	}

	private static String format(String formatStr, LocalDateTime date) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatStr);
		return formatter.format(date);
	}

	public static String formatYYYYMMDD() {
		return format(YYYY_MM_DD, new Date());
	}

	public static String formatYYYYMD() {
		return format(YYYY_M_D, new Date());
	}

	//YYYY/MM/DD
	public static String formatYYYYMMDD2(){
		return format(YYYY_MM_DD2, new Date());
	}
	
	public static String formatYYYYMMDDHHMMSS(Date date){
		return format(YYYY_MM_DD_HH_MM_SS, date);
	}

	public static String formatYYYYMMDDHHMMSS(LocalDateTime date){
		return format(YYYY_MM_DD_HH_MM_SS, date);
	}

	public static String formatYYYYMMDD (Date date){
		return format(YYYY_MM_DD, date);
	}
	//20170221090835
	public static String formatYYYYMMDDHHMMSSSimple(){
		return format(YYYYMMDDHHMMSS, new Date());
	}

	public static String formatYYYYMMMDDSimmple(){
		return format(YYYYMMMDD_SIMPLE, new Date());
	}

	public static String formatYYYYMMMDDSimmple(String date){
		DateFormat df1 = new SimpleDateFormat(YYYYMMDDHHMMSS);
		Date date1 = null;
		try {
			date1 = df1.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		df1 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		String time = df1.format(date1);
		return time;
	}

	public static String getYYYY(){
		Calendar now = Calendar.getInstance();
		return String.valueOf(now.get((Calendar.YEAR)));
	}
	public static String getMM(){
		Calendar now = Calendar.getInstance();
		return String.valueOf(now.get(Calendar.MONTH) + 1);
	}
	//将时间字符串转化为时间格式
	public static Date formatDateYYYYMMDD(String date){
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		Date parse = new Date();
		try {
			parse = formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parse;
	}

	public static Date formatDateYYYYMMDDHHMMSS(String date){
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		Date parse = new Date();
		try {
			parse = formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parse;
	}

	public static Date formatDateYYYYMMDDSimple(){
		String yyyymmdd = formatYYYYMMDD();
		return formatDateYYYYMMDD(yyyymmdd);
	}
}
